/*
 * Copyright 2011 Katsunori Koyanagi
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package sweezy.core.reflect
{
	import flash.system.ApplicationDomain;
	import flash.utils.getQualifiedClassName;

	public class Reflection
	{

		reflect_internal static var factory:ITypeFactory = new DefaultTypeFactory();

		public static function get factory():ITypeFactory
		{
			return reflect_internal::factory;
		}
		
		public static function clearCache():void{
			reflect_internal::factory.clearCache();
		}

		public static function forClass(clazz:Class, applicationDomain:ApplicationDomain = null):IType
		{
			if (clazz === null)
			{
				return null;
			}

			var className:String = getQualifiedClassName(clazz);
			return forName(className, applicationDomain);
		}

		public static function forInstance(instance:*, applicationDomain:ApplicationDomain = null):IType
		{
			if (instance === undefined || instance === null)
			{
				return null;
			}

			var className:String = getQualifiedClassName(instance);
			return forName(className, applicationDomain);
		}

		public static function forName(className:String, applicationDomain:ApplicationDomain = null):IType
		{
			applicationDomain = applicationDomain === null ? ApplicationDomain.currentDomain : applicationDomain;
			return reflect_internal::factory.createType(className, applicationDomain);
		}
	}
}
